package org.oaix.hapi;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.app.HL7Service;
import ca.uhn.hl7v2.app.ServerConfiguration;
import ca.uhn.hl7v2.llp.MinLowerLayerProtocol;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class BasicListenerWithoutMessageHandling {

    private static int PORT_NUMBER  = 8080;
    private static HapiContext context = new DefaultHapiContext();

    public static void main(String[] args) throws Exception {
        if(args.length > 0){
            PORT_NUMBER = Integer.parseInt(args[0]);
        }
        try {
            ThreadPoolExecutor executor = new ThreadPoolExecutor(
                    10, 100,
                    30, TimeUnit.SECONDS,
                    new ArrayBlockingQueue<Runnable>(100));
            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

            MinLowerLayerProtocol mllp = new MinLowerLayerProtocol();
            mllp.setCharset("UTF-8");
            context.setLowerLayerProtocol(mllp);
            context.setExecutorService(executor);
            ServerConfiguration s = new ServerConfiguration();
            boolean useSecureConnection = false; // are you using TLS/SSL?
            HL7Service ourHl7Server = context.newServer(PORT_NUMBER, useSecureConnection);
            ourHl7Server.registerApplication(new MyReceivingApplication());
            ourHl7Server.startAndWait();
//            System.out.println("MLLP-HL7消息接收服务启动成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
